package com.mach.platform.service;

import com.mach.platform.utils.DateUtil;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 业务工具类
 与业务相关的工具逻辑在此类编写
 */
public class UServ {

    public static Map<Integer, String> timeLevelValueMapSQLField = new HashMap<Integer, String>();
    public static Map<Integer, String> timeLevelValueMapSQLFmt = new HashMap<Integer, String>();

    static{
        timeLevelValueMapSQLField.put(1,"fsec");
        timeLevelValueMapSQLField.put(2,"fmin");
        timeLevelValueMapSQLField.put(3,"fhour");
        timeLevelValueMapSQLField.put(4,"fday");
        timeLevelValueMapSQLField.put(5,"fmon");
        timeLevelValueMapSQLField.put(6,"fyear");

        timeLevelValueMapSQLFmt.put(1,"%Y-%m-%d %H:%i:%s");
        timeLevelValueMapSQLFmt.put(2,"%Y-%m-%d %H:%i");
        timeLevelValueMapSQLFmt.put(3,"%Y-%m-%d %H");
        timeLevelValueMapSQLFmt.put(4,"%Y-%m-%d");
        timeLevelValueMapSQLFmt.put(5,"%Y-%m");
        timeLevelValueMapSQLFmt.put(6,"%Y");

    }

    
    /**
     * 通过时间返回时间内对应分段数组
     * @param time
     * @return
     */
    public static String[] getTimeArrByTime(String time) throws ParseException {
        int arrLength;
        if (time.length() == 4) {
            arrLength = 12;
        } else if (time.length() == 7) {
            // 获取某个月份的天数
            Calendar calendar = Calendar.getInstance();
            Date date = DateUtil.getDateByStrTime(time, "yyyy-MM");
            calendar.setTime(date);
            arrLength = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        } else if (time.length() == 10) {
            arrLength = 24;
        } else if (time.length() == 13) {
            arrLength = 60;
        } else if (time.length() == 16) {
            arrLength = 60;
        } else {
            arrLength = 0;
        }
        String[] timeArr = new String[arrLength];
        for (int i = 1; i <= arrLength; i++) {
            // 月份和天数的初始值是01
            if (time.length() == 4 || time.length() == 7) {
                if (i < 10) {
                    timeArr[i - 1] = "0" + i;
                } else {
                    timeArr[i - 1] = "" + i;
                }
            } else {
                // 小时，分钟，秒的初始值是00
                if (i < 11) {
                    timeArr[i - 1] = "0" + (i - 1);
                } else {
                    timeArr[i - 1] = "" + (i - 1);
                }
            }
        }
        return timeArr;
    }


    /**
     * 获取环比的时间(对比上一年，上个月，上一天)
     * @param date 时间
     * @return
     */
    public static String getRingTime(String date) {
        if (date.length() == 4) {
            return "" + (Integer.parseInt(date) - 1);
        }
        String format = (date.length() == 7 ? "yyyy-MM" : "yyyy-MM-dd");
        DateFormat df = new SimpleDateFormat(format);
        try {
            Date time = df.parse(date);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(time);
            int dateType = (date.length() == 7 ? Calendar.MONTH : Calendar.DATE);
            calendar.add(dateType, -1);
            return df.format(calendar.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 保留小数
     * @param value 目标值
     * @param bitNum 位数 0：一位小数 00：两位小数 依次类推
     * @return
     */
    public static double retainDecimal(double value, String bitNum) {
        DecimalFormat df = new DecimalFormat("#." + bitNum);
        return Double.parseDouble(df.format(value));
    }


    /**
     * 根据时间获取时间参数为：年, 月,日
     * @param time
     * @return
     */
    public static String getTimeByFormat(String time) {
        if (time.length() == 4) {
            return "year";
        } else if (time.length() == 7) {
            return "month";
        } else if (time.length() == 10){
            return "day";
        }else if (time.length() == 13){
            return "hour";
        } else if (time.length() == 16) {
            return "minute";
        } else {
            return "other";
        }
    }


    /**
     * 通过时间获取需要格式化的SQL时间粒度
     * @param time
     * @return
     */
    public static String getPatternTime(String time) {
        if (time.length() == 4) { // 年
            return "%m";
        } else if (time.length() == 7) { // 月
            return "%d";
        } else if (time.length() == 10){ // 日
            return "%H";
        }else if (time.length() == 13){ // 小时
            return "%i";
        } else if (time.length() == 16) { // 分钟
            return "%s";
        } else {
            return "other";
        }
    }


    /**
     * 通过时间获取需要格式化的SQL时间粒度
     * @param time
     * @return
     */
    public static String getFormatTime(String time) {
        if (time.length() == 4) { // 年
            return "%Y-%m";
        } else if (time.length() == 7) { // 月
            return "%Y-%m-%d";
        } else if (time.length() == 10){ // 日
            return "%Y-%m-%d %H";
        }else if (time.length() == 13){ // 小时
            return "%Y-%m-%d %H:%m";
        } else if (time.length() == 16) { // 分钟
            return "%Y-%m-%d %H:%m:%s";
        } else {
            return "other";
        }
    }


    /**
     * 通过时间获取需要格式化的Data时间粒度
     * @param time
     * @return
     */
    public static String getFormatTimeData(String time) {
        if (time.length() == 4) { // 年
            return "yyyy";
        } else if (time.length() == 7) { // 月
            return "yyyy-MM";
        } else if (time.length() == 10){ // 日
            return "yyyy-MM-dd";
        }else if (time.length() == 13){ // 小时
            return "yyyy-MM-dd HH";
        } else if (time.length() == 16) { // 分钟
            return "yyyy-MM-dd HH:mm";
        } else {
            return "other";
        }
    }


    public static String map4TimeLevel2SqlFmt(Integer timeLevel){
        String fmt = timeLevelValueMapSQLFmt.get(timeLevel);
        if(null == fmt){
            fmt = "'%Y-%m-%d %H:%i";
        }
        return fmt;
    }

}
